import * as PageTimelinesService from '../../services/Editor/pageTimelines';

export default {
    namespace: 'pageTimelines',
    state: [],
    reducers: {
        addTimelinesOfPage(oldTimelines, { payload: pTimelines }) {
            const timelines = oldTimelines.concat(pTimelines);
            return timelines;
        },
        updatePageTimeline(oldTimelines, { payload: timeline }) {
            if (!timeline.page_id) return oldTimelines;
            const sTimelines = oldTimelines.filter(w => w.page_id === timeline.page_id);
            if (sTimelines.length > 0) {
                const sIndex = oldTimelines.indexOf(sTimelines[0]);
                const tWidget = { ...sTimelines[0], ...timeline };
                oldTimelines[sIndex] = tWidget;
            }else {
                oldTimelines.push(timeline);
            }
            return oldTimelines;
        },
    },
    effects: {
        /**
         * 新增page_timeline时调用
         * 注意：修改时请调用updateTimeline以便本地化保存
         */
        *addPageTimeline({ payload: timeline }, { call, put }) {
            const { data: pageTimeline } = yield call(PageTimelinesService.update, timeline);
            pageTimeline.page_id = timeline.page_id;
            yield put({ type: 'addTimelinesOfPage', payload: pageTimeline });
        },
    },
    subscriptions: {

    },
};
